import commonData from "./common_data";
import tiyData from "./edit_data";
import {confirmOfflineStands, confirmPublishStands, queryStandsList} from "./list_funs";
import productData from "../../productSelect/js/productData";
import {initProductPage, initProductParams} from "../../productSelect/js/productFuns";
import {isEmpty, isNotEmpty} from "../../../js/commonUtils";
import {
  getDescPageMetaList, getKeywordsPageMetaList,
  getTitlePageMetaList,
  initPageMetaInfo,
  resetPageMetaInfo
} from "../../PageMetaForm/js/pageMetaFuns";
import pageMetaData from "../../PageMetaForm/js/pageMetaData";
import common_data from "./common_data";
import ApiBase from "../../../../api/moudles/ApiBase";
import {replaceHtmlTagAttr} from "../../../widgets/quill/extension";

/**重置页面数据*/
export const resetPageInfo = (self) => {
  self.searchPlatform = 0;
  self.isDisplayBaseInfo = false;
  self.activeTab = "boothList";
}

export const initBoothListSearchParam = (showroomId) => {
  tiyData.tParam.searchParam.booth.displayType = 0;
  tiyData.tParam.searchParam.booth.platforms.splice(0);
  tiyData.tParam.searchParam.booth.platforms.push(...[commonData.standsDic.pcPlatform, commonData.standsDic.appPlatform, commonData.standsDic.unionPlatform]);
}

export const initProductAddedListSearchParam = () => {
  tiyData.tParam.searchParam.addedProducts.search = "";
  tiyData.tParam.searchParam.addedProducts.categoryLevel = null;
  tiyData.tParam.searchParam.addedProducts.categoryId = "";
  tiyData.tParam.searchParam.addedProducts.state = null;
  tiyData.tParam.searchParam.addedProducts.selectedDate = null;
  tiyData.tParam.searchParam.addedProducts.startTime = null;
  tiyData.tParam.searchParam.addedProducts.endTime = null;
}

/**根据ID查询展厅*/
export const initStandsInfo = (self, standsId, ifInitBooths, initProducts) => {
  let params = {id: standsId};
  self.$Spin.show();
  self.$apiStand.queryStandsById(params).then(response => {
    if(response.success) {
      let stands = response.result;
      if(stands == null) {
        self.$Message.error("未查询到数据，展厅可能不存在或已被删除！");
        return;
      }
      //基本信息
      initBaseInfo(self, stands);
      //展区列表
      if(ifInitBooths) {
        queryBoothList(self);
      }
      if(initProducts && stands.hasProductArea) {
        if(stands.collection.type==commonData.collectionDic.customType) { //自定义关联类型才有可管理产品列表
          queryAddedProductList(self);
        }
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**初始化基本信息*/
export const initBaseInfo = (self, stands) => {
  tiyData.tData.baseInfo.id = stands.id;
  tiyData.tData.baseInfo.title = stands.title;
  tiyData.tData.baseInfo.subtitle = stands.subtitle;
  tiyData.tData.baseInfo.description = stands.description;
  tiyData.tData.baseInfo.name = stands.name;
  tiyData.tData.baseInfo.platform = stands.platform;
  tiyData.tData.baseInfo.type = stands.type;
  tiyData.tData.baseInfo.state = stands.state;
  tiyData.tData.baseInfo.publishState = stands.publishState;
  tiyData.tData.baseInfo.shortLink = stands.shortLink;
  if(isNotEmpty(stands.shortLink)) {
    tiyData.tData.baseInfo.shortLinkName = stands.shortLink.substring(0, stands.shortLink.lastIndexOf(".html"));
  }else{
    tiyData.tData.baseInfo.shortLinkName = "";
  }

  tiyData.tData.baseInfo.tags.splice(0);
  if(isNotEmpty(stands.tags)) {
    tiyData.tData.baseInfo.tags.push(...self.$toJson(stands.tags));
  }
  tiyData.tData.baseInfo.note = stands.note;
  tiyData.tData.baseInfo.createtime = stands.createtime;
  tiyData.tData.baseInfo.lastModifyTime = stands.lastModifyTime;
  tiyData.tData.baseInfo.lastPublishTime = stands.lastPublishTime;
  tiyData.tData.baseInfo.hasProductArea = stands.hasProductArea;
  tiyData.tData.baseInfo.classificationLevel = stands.classificationLevel;
  tiyData.tData.baseInfo.classificationId = stands.classificationId;
  tiyData.tData.baseInfo.categoryList.splice(0);
  if(stands.categoryList!=null && stands.categoryList.length>0) {
    tiyData.tData.baseInfo.categoryList.push(...stands.categoryList);
  }
  if(stands.hasProductArea && stands.collection != null) {
    tiyData.tData.baseInfo.collectionId = stands.collection.id;
    tiyData.tData.baseInfo.collectionTitle = stands.collection.title;
    tiyData.tData.baseInfo.collectionType = stands.collection.type;
    // tiyData.tData.baseInfo.collectionKeyword = stands.collection.keyword;

    tiyData.tData.baseInfo.collectionFilters.splice(0);
    if(isNotEmpty(stands.collection.filters)) {
      tiyData.tData.baseInfo.collectionFilters.push(...self.$toJson(stands.collection.filters));
    }
    tiyData.tData.baseInfo.collectionClassificationId = stands.collection.classificationId;
    tiyData.tData.baseInfo.collectionHasCategory = isNotEmpty(tiyData.tData.baseInfo.collectionClassificationId);

    tiyData.tData.baseInfo.collectionHasKeyword = stands.collection.addKeyword==1;
    if(tiyData.tData.baseInfo.collectionHasKeyword){
      tiyData.tData.baseInfo.collectionKeyword = stands.collection.keyword;
    }else{
      tiyData.tData.baseInfo.collectionHasKeyword="";
    }

    tiyData.tData.baseInfo.collectionHasDate = stands.collection.addDateSearch==1;
    if(tiyData.tData.baseInfo.collectionHasDate){
      tiyData.tData.baseInfo.collectionPublishedStartDate = stands.collection.startDate;
      tiyData.tData.baseInfo.collectionPublishedEndDate = stands.collection.endDate;
    }else{
      tiyData.tData.baseInfo.collectionPublishedStartDate = "";
      tiyData.tData.baseInfo.collectionPublishedEndDate = "";
    }
    tiyData.tData.baseInfo.collectionReturnFilters.splice(0);
    if(isNotEmpty(stands.collection.returnFilters)) {
      let returnFilters = self.$toJson(stands.collection.returnFilters).hide;
      if(returnFilters != null) {
        tiyData.tData.baseInfo.collectionReturnFilters.push(...returnFilters);
      }
    }
  }else{
    tiyData.tData.baseInfo.collectionId = "";
    tiyData.tData.baseInfo.collectionTitle = "";
    tiyData.tData.baseInfo.collectionType = 0;
    tiyData.tData.baseInfo.collectionKeyword = "";
    tiyData.tData.baseInfo.collectionFilters.splice(0);
  }
  tiyData.tData.baseInfo.disableSeo = stands.disableSeo;
  tiyData.tData.baseInfo.pageMetaInfos.splice(0);
  tiyData.tData.baseInfo.pageMetaInfos.push(...stands.pageMetaInfos);
}

/**重置修改参数*/
export const resetEditParam = (self) => {
  self.$refs.AStandModify.$refs.standsBaseInfoForm.resetFields();
}

/**初始化修改参数*/
export const initEditParam = (self, stands) => {
  tiyData.tParam.editParam.stands.id = stands.id;
  tiyData.tParam.editParam.stands.title = stands.title;
  tiyData.tParam.editParam.stands.subtitle = stands.subtitle;
  tiyData.tParam.editParam.stands.description = stands.description;
  tiyData.tParam.editParam.stands.name = stands.name;
  tiyData.tParam.editParam.stands.platform = stands.platform;
  tiyData.tParam.editParam.stands.type = stands.type;
  // tiyData.tParam.editParam.stands.state = stands.state;
  tiyData.tParam.editParam.stands.shortLink = stands.shortLink;
  tiyData.tParam.editParam.stands.shortLinkName = stands.shortLinkName;
  tiyData.tParam.editParam.stands.tags.splice(0);
  commonData.standsDic.tags.splice(0);
  if(stands.tags!=null && stands.tags.length>0) {
    tiyData.tParam.editParam.stands.tags.push(...stands.tags);
    commonData.standsDic.tags.push(...stands.tags);
  }
  tiyData.tParam.editParam.stands.note = stands.note;
  tiyData.tParam.editParam.stands.hasProductArea = stands.hasProductArea;
  tiyData.tParam.editParam.stands.classificationId = stands.classificationId;
  tiyData.tParam.editParam.stands.classificationLevel = stands.classificationLevel;
  tiyData.tParam.editParam.stands.categoryList.splice(0);
  if(stands.categoryList!=null && stands.categoryList.length>0) {
    for(let cat of stands.categoryList) {
      tiyData.tParam.editParam.stands.categoryList.push(cat.id);
    }
  }
  if(stands.hasProductArea) {
    if(isNotEmpty(stands.collectionId)) {
      tiyData.tParam.editParam.stands.collectionId = stands.collectionId;
      tiyData.tParam.editParam.stands.collectionTitle = stands.collectionTitle;
      tiyData.tParam.editParam.stands.collectionType = stands.collectionType;
      tiyData.tParam.editParam.stands.collectionFilters.splice(0);
      tiyData.tParam.editParam.stands.collectionReturnFilters.splice(0);
      tiyData.tParam.editParam.stands.collectionClassificationId = stands.collectionClassificationId;
      if(stands.collectionType!=commonData.collectionDic.customType){
        if(stands.collectionHasCategory) {
          tiyData.tParam.editParam.stands.collectionHasCategory = true;
          tiyData.tParam.editParam.stands.collectionClassificationId = stands.collectionClassificationId;
        }else{
          tiyData.tParam.editParam.stands.collectionHasCategory = false;
          tiyData.tParam.editParam.stands.collectionClassificationId = "";
        }
        if(stands.collectionHasKeyword){
          tiyData.tParam.editParam.stands.collectionHasKeyword = true;
          tiyData.tParam.editParam.stands.collectionKeyword = stands.collectionKeyword;
        }else{
          tiyData.tParam.editParam.stands.collectionHasKeyword = false;
          tiyData.tParam.editParam.stands.collectionKeyword = "";
        }
        if(stands.collectionHasDate){
          tiyData.tParam.editParam.stands.collectionHasDate = true;
          tiyData.tParam.editParam.stands.collectionPublishedStartDate = stands.collectionPublishedStartDate;
          tiyData.tParam.editParam.stands.collectionPublishedEndDate = stands.collectionPublishedEndDate;
        }else{
          tiyData.tParam.editParam.stands.collectionHasDate = false;
          tiyData.tParam.editParam.stands.collectionPublishedStartDate = "";
          tiyData.tParam.editParam.stands.collectionPublishedEndDate = "";
        }
        tiyData.tParam.editParam.stands.collectionFilters.push(...stands.collectionFilters);
        tiyData.tParam.editParam.stands.collectionReturnFilters.push(...stands.collectionReturnFilters);
      }
      // if(stands.collectionType == commonData.collectionDic.keywordType || stands.collectionType == commonData.collectionDic.dateRangeType) {
      //   tiyData.tParam.editParam.stands.collectionKeyword = stands.collectionKeyword;
      //   tiyData.tParam.editParam.stands.collectionFilters.push(...stands.collectionFilters);
      // }else{
      //   tiyData.tParam.editParam.stands.collectionKeyword = "";
      // }
    }
  } else {
    tiyData.tParam.editParam.stands.collectionId = "";
    tiyData.tParam.editParam.stands.collectionTitle = "";
    tiyData.tParam.editParam.stands.collectionType = 0;
    tiyData.tParam.editParam.stands.collectionKeyword = "";
    self.$ySpliceAll(tiyData.tParam.editParam.stands.collectionFilters);
  }

  tiyData.tParam.editParam.stands.disableSeo = stands.disableSeo;
}

export const showModifyStandsModal = (self, stands) => {
  tiyData.tStatus.stands.modalModify.isShow = true;
  resetEditParam(self);
  initEditParam(self, stands);
}

export const showModifyHtmlStandsModal = (self, stands) => {
  tiyData.tStatus.stands.modalModifyHtml.isShow = true;
  resetHtmlEditParam(self);
  initHtmlEditParam(self, stands);
}

export const resetHtmlEditParam = (self) => {
  self.$refs.AStandModifyHtml.$refs.standsBaseInfoForm.resetFields();
}

export const initHtmlEditParam = (self, stands) => {
  tiyData.tParam.editParam.htmlStands.id = stands.id;
  tiyData.tParam.editParam.htmlStands.name = stands.name;
  tiyData.tParam.editParam.htmlStands.platform = stands.platform;
  tiyData.tParam.editParam.htmlStands.type = stands.type;
  tiyData.tParam.editParam.htmlStands.shortLink = stands.shortLink;
  tiyData.tParam.editParam.htmlStands.shortLinkName = stands.shortLinkName;
  tiyData.tParam.editParam.htmlStands.tags.splice(0);
  commonData.standsDic.tags.splice(0);
  if(stands.tags!=null && stands.tags.length>0) {
    tiyData.tParam.editParam.htmlStands.tags.push(...stands.tags);
    commonData.standsDic.tags.push(...stands.tags);
  }
  tiyData.tParam.editParam.htmlStands.note = stands.note;
  tiyData.tParam.editParam.htmlStands.disableSeo = stands.disableSeo;
}

/**修改展厅
 * @param callback 回调函数
 * */
export const modifyStands = (self, callback) => {
  let params = tiyData.tParam.editParam.stands;
  self.$refs.AStandModify.$refs.standsBaseInfoForm.validate(valid=> {
    if(valid) {
      if(isNotEmpty(tiyData.tParam.editParam.stands.shortLink) && tiyData.tParam.editParam.stands.shortLink.indexOf("wholesale/")>=0){
        self.$Modal.warning({
          title: "警告",
          content: '固定网址不能包含"wholesale/"!'
        });
        return;
      }

      if(params.collectionType!=commonData.collectionDic.categoryType&&params.collectionType!=commonData.collectionDic.customType&&params.hasProductArea && !params.collectionHasDate && !params.collectionHasKeyword && !params.collectionClassificationId) {
        self.$Modal.warning({title: "警告", content:"请至少填写一种条件！"});
        return;
      }

      //处理日期
      if(params.collectionPublishedStartDate && params.collectionPublishedStartDate instanceof Object){
        var localDate = params.collectionPublishedStartDate;
        params.collectionPublishedStartDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }
      if(params.collectionPublishedEndDate && params.collectionPublishedEndDate instanceof Object){
        var localDate = params.collectionPublishedEndDate;
        params.collectionPublishedEndDate = new Date(Date.UTC(localDate.getFullYear(), localDate.getMonth(), localDate.getDate(),0,0,0)).toJSON();
      }
      self.$Spin.show();
      self.$apiStand.updateStands(params).then(response => {
        if(response.success) {
          if(callback) {
            callback(self, params.id);
          }
          self.$Message.success("修改保存成功！");
          tiyData.tStatus.stands.modalModify.isShow = false;
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }else{
      console.log("validate failed");
    }
  });

}

/**修改展厅基本信息之后*/
export const afterModifyStandsBaseInfo = (self) => {
  let standsId = tiyData.tData.baseInfo.id;
  initStandsInfo(self, standsId, false, true);
  queryStandsList(self);
}

export const modifyHtmlStands = (self, callback) => {
  let params = tiyData.tParam.editParam.htmlStands;
  self.$refs.AStandModifyHtml.$refs.standsBaseInfoForm.validate(valid=> {
    if(valid) {
      if (isNotEmpty(params.shortLink) && params.shortLink.indexOf("wholesale/") >= 0) {
        self.$Modal.warning({
          title: "警告",
          content: '固定网址不能包含"wholesale/"!'
        });
        return;
      }
      self.$Spin.show();
      self.$apiStand.updateHtmlStands(params).then(response => {
        if(response.success) {
          if(callback) {
            callback(self, params.id);
          }
          self.$Message.success("修改保存成功！");
          tiyData.tStatus.stands.modalModifyHtml.isShow = false;
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }else{
      console.log("validate failed");
    }
  });
}

export const afterModifyHtmlStandsBaseInfo = (self) => {
  let standsId = tiyData.tData.htmlBaseInfo.id;
  initHtmlStandsInfo(self, standsId);
}

/**查询展区列表*/
export const queryBoothList = (self) => {
  tiyData.tParam.searchParam.booth.showroomId = tiyData.tData.baseInfo.id;
  let params = {
    showroomId: tiyData.tParam.searchParam.booth.showroomId,
  };
  if(tiyData.tParam.searchParam.booth.platforms!=null && tiyData.tParam.searchParam.booth.platforms.length>0) {
    params.platforms = tiyData.tParam.searchParam.booth.platforms;
  }
  if(tiyData.tParam.searchParam.booth.displayType != 0) {
    params.displayType = tiyData.tParam.searchParam.booth.displayType;
  }
  self.$Spin.show();
  self.$apiStand.queryBoothList(params).then(response => {
    tiyData.tData.boothList.splice(0);
    if(response.success) {
      tiyData.tData.boothList.push(...response.result);
    }else{
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**查询产品集合列表*/
export const queryAddedProductList = (self) => {
  tiyData.tParam.searchParam.addedProducts.collectionId = tiyData.tData.baseInfo.collectionId;
  let params = tiyData.tParam.searchParam.addedProducts;
  params.pageNum = tiyData.tPage.productsAddedPage.pageNum;
  params.pageSize = tiyData.tPage.productsAddedPage.pageSize;
  self.$Spin.show();
  self.$apiStand.queryAddedProductList(params).then(response => {
    tiyData.tData.productAddedList.splice(0);
    if(response.success) {
      let page = response.result;
      tiyData.tData.productAddedList.push(...page.list);
      tiyData.tPage.productsAddedPage.total = page.total;
    }else{
      tiyData.tPage.productsAddedPage.total = 0;
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**查询排序产品集合列表*/
export const querySortAddedProductList = (self) => {
  self.$Spin.show();
  let params = {
    "collectionId": tiyData.tData.baseInfo.collectionId,
  };
  self.$apiStand.queryAllAddedProductList(params).then(response => {
    self.$Spin.hide();
    self.$ySpliceAll(tiyData.tData.productAllAddedList);
    if(response.success) {
        response.result.forEach(function(item){
        tiyData.tData.productAllAddedList.push(item);
      });
      tiyData.tStatus.modalSort.isShow = true;
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

/**
 * 保存需要排序的数据
 * @param self
 * @param tiyData
 */
export const webSortDoing = (self, tiyData) => {
  tiyData.tStatus.modalSort.isLoading = true;
  self.$Spin.show();
  let productList=tiyData.tData.productAllAddedList;
  productList.forEach(function (item){
    delete item.product;
  })
  let params={
    "productAllAddedList":productList
  }
  self.$apiStand.saveBatchSort(params).then(function (response) {
    if (response.success) {
      self.$Message.info("保存成功!");
      self.searchAddedProducts();
      self.$Spin.hide();
    } else {
      self.$Message.error(response.errorMessage);
      self.$Spin.hide();
    }
  });
};

/**显示添加展区窗口*/
export const showAddBoothModal = (self) => {
  resetAddBoothParam(self);
  tiyData.tParam.addParam.booth.platform = tiyData.tData.baseInfo.platform;
  tiyData.tParam.addParam.booth.showroomId = tiyData.tData.baseInfo.id;
  tiyData.tStatus.booth.modalAdd.isShow = true;
}

/**重置新增展区表单*/
export const resetAddBoothParam = (self) => {
  self.$refs.BoothAdd.$refs.boothInfoForm.resetFields();
}

/**添加展区
 * @param callback 回调函数
 * */
export const addBooth = (self) => {
  self.$refs.BoothAdd.$refs.boothInfoForm.validate(valid => {
    if(valid) {
      tiyData.tParam.addParam.booth.showroomId = tiyData.tData.baseInfo.id;
      let params = tiyData.tParam.addParam.booth;
      if(params.displayType == commonData.boothDic.htmlType) {
        params.htmlContent = replaceHtmlTagAttr(params.htmlContent, "img", "width", "auto");
        params.htmlContent = replaceHtmlTagAttr(params.htmlContent, "img", "height", "auto");
      }
      self.$Spin.show();
      self.$apiStand.saveBooth(params).then(response => {
        if(response.success) {
          self.$Message.success("保存新增展区成功！");
          afterSaveBooth(self, response.result);
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

export const afterSaveBooth = (self, boothId) => {
  tiyData.tStatus.booth.modalAdd.isShow = false;
  queryBoothList(self);
  //刷新展厅信息
  afterModifyStandsBaseInfo(self);
  showModifyBoothModal(boothId);
}

/**删除展区
 * @param callback 回调函数
 * */
export const deleteBooth = (self, boothId) => {
  self.$Modal.confirm({
    title: "确认",
    content: "确定要删除此展区吗？",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      let params = {id: boothId};
      self.$Spin.show();
      self.$apiStand.deleteBooth(params).then(response => {
        if(response.success) {
          self.$Message.success("已经删除！");
          queryBoothList(self);
          //刷新展厅信息
          afterModifyStandsBaseInfo(self);
        }else{
          self.$Message.success(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

/**显示编辑展区弹窗*/
export const showModifyBoothModal = (boothId) => {
  tiyData.tParam.editParam.booth.id = boothId;
  tiyData.tParam.editParam.booth.modifyingTime = new Date().getTime();
  tiyData.tStatus.booth.modalModify.isShow = true;
}

/**显示新增产品条目弹窗*/
export const showAddProductsModal = (self) => {
  //重置产品查询参数
  initProductParams();
  //重置产品列表分页
  initProductPage();
  //重置新增产品参数
  resetProductAddParam();
  // queryProductList(self);
  //显示弹窗
  tiyData.tStatus.products.modalAdd.isShow = true;
}

export const resetProductAddParam = () => {
  tiyData.tParam.addParam.products.collectionId = tiyData.tData.baseInfo.collectionId;
  tiyData.tParam.addParam.products.productIds.splice(0);
}

export const queryProductList = (self) => {
  let params = productData.tParam;
  params.collectionId = tiyData.tData.baseInfo.collectionId;
  params.pageNum = productData.tPage.pageNum;
  params.pageSize = productData.tPage.pageSize;
  self.$Spin.show();
  self.$apiStand.queryProductList(params).then(response => {
    productData.tData.list.splice(0);
    if(response.success) {
      let page = response.result;
      productData.tData.list.push(...page.list);
      productData.tPage.total = page.total;
    }else{
      productData.tPage.total = 0;
      self.$Message.error(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

/**添加产品*/
export const addProducts = (self, callback) => {
  if(productData.tData.selectedList==null || productData.tData.selectedList.length==0) {
    self.$Message.error("请选择要添加的产品！");
    return;
  }
  tiyData.tParam.addParam.products.productIds = productData.tData.selectedList.map(product => {return product.id});
  let params = tiyData.tParam.addParam.products;
  self.$Spin.show();
  self.$apiStand.addProducts(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("添加产品成功！");
      tiyData.tStatus.products.modalAdd.isShow = false;
      tiyData.tStatus.products.modalSequence.isShow = false;
      if(callback) {
        callback(self);
      }
    }else {
      self.$Message.error(response.errorMessage);
    }
  }).catch(err => {
    self.$Spin.hide();
    self.$Message.error("添加产品失败！");
  });
}

/**直接向展区添加产品成功之后*/
export const afterAddProducts = (self) => {
  productData.tData.selectedList.splice(0);
  afterModifyStandsBaseInfo(self);
  queryProductList(self);
  queryAddedProductList(self);
}

/**修改产品序号*/
export const modifyProduct = (self, callback) => {
  self.$refs.modifyProductForm.validate(valid => {
    if(valid) {
      let params = tiyData.tParam.editParam.product;
      self.$Spin.show();
      self.$apiStand.modifyProduct(params).then(response => {
        if(response.success) {
          tiyData.tStatus.products.modalModify.isShow = false;
          self.$Message.success("修改保存成功！");
          if(callback) {
            callback(self);
          }
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

/**修改产品条目成功之后*/
export const afterModifyProduct = (self) => {
  afterModifyStandsBaseInfo(self);
  tiyData.tParam.searchParam.addedProducts.collectionId = tiyData.tData.baseInfo.collectionId;
  queryAddedProductList(self);
}

/**删除产品条目*/
export const deleteProduct = (self, item, callback) => {
  let params = {
    collectionId: item.collectionId,
    productId: item.productId,
  };
  self.$Modal.confirm({
    title: "确认",
    content: "确定要删除此产品条目吗？",
    okText: "删除",
    cancelText: "取消",
    onOk() {
      self.$Spin.show();
      self.$apiStand.deleteProduct(params).then(response => {
        if(response.success) {
          self.$Message.success("产品条目删除成功！");
          if(callback) {
            callback(self);
          }
        }else{
          self.$Message.error(response.errorMessage);
        }
        self.$Spin.hide();
      });
    }
  });
}

/**删除展区直接关联的产品条目成功之后*/
export const afterDeleteProduct = (self) => {
  afterModifyStandsBaseInfo(self);
  tiyData.tParam.searchParam.addedProducts.collectionId = tiyData.tData.baseInfo.collectionId;
  queryAddedProductList(self);
}

export const showModifySEOModal = (self, stands) => {
  // resetSEOEditParam();
  initSEOEditParam(stands);
  tiyData.tStatus.stands.modalModifySEO.isShow = true;
}

export const showModifyHtmlSEOModal = (self, stands) => {
  // resetSEOEditParam();
  initSEOEditParam(stands);
  tiyData.tStatus.stands.modalModifyHtmlSEO.isShow = true;
}

export const resetSEOEditParam = () => {
  resetPageMetaInfo();
}

export const initSEOEditParam = (stands) => {
  initPageMetaInfo(stands.id, pageMetaData.pageMetaDic.showroomType, stands.pageMetaInfos);
}

export const saveStandsSEO = (self, showroomType) => {
  let pageMetaInfos = [];
  pageMetaInfos.push(...getTitlePageMetaList());
  pageMetaInfos.push(...getDescPageMetaList());
  pageMetaInfos.push(...getKeywordsPageMetaList());
  let params = {
    showroomId: showroomType == commonData.standsDic.htmlType?tiyData.tData.htmlBaseInfo.id : tiyData.tData.baseInfo.id,
    pageMetaInfos: pageMetaInfos
  }

  self.$Spin.show();
  self.$apiStand.savePageMetaInfos(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("保存成功！");
      if(showroomType == commonData.standsDic.htmlType) {
        tiyData.tStatus.stands.modalModifyHtmlSEO.isShow = false;
        initHtmlStandsInfo(self, tiyData.tData.htmlBaseInfo.id);
      }else{
        tiyData.tStatus.stands.modalModifySEO.isShow = false;
        //刷新展厅信息
        initStandsInfo(self, tiyData.tData.baseInfo.id, false, true);
      }
    }else{
      self.$Message.error(response.errorMessage);
    }
  })
}

/**下线展厅
 * @param callback 回调函数
 * */
export const beforeOfflineStands = (self, standId, callback) => {
  let params = {id: standId, state: commonData.standsDic.publishedState};
  self.$Spin.show();
  self.$apiStand.queryParentShowroomList(params).then(response => {
    self.$Spin.hide();
    if(response.result == null || response.result.length == 0) {//没有关联已发布展厅
      confirmOfflineStands(self, standId, callback);
    }else{
      tiyData.tParam.parentsParam.showroomId = standId;
      tiyData.tParam.parentsParam.currentTime = new Date().getTime();
      tiyData.tStatus.modalParents.isShow = true;
    }
  });
}

/**发布展厅
 * @param callback 回调函数
 * */
export const beforePublishStands = (self, standId, callback) => {
  let params = {id: standId, state: commonData.standsDic.unPublishedState};

  self.$Spin.show();
  self.$apiStand.queryChildShowroomList(params).then(response => {
    self.$Spin.hide();
    if(response.result == null || response.result.length == 0) {//没有关联已发布展厅
      confirmPublishStands(self, standId, callback);
    }else{
      tiyData.tParam.childrenParam.showroomId = standId;
      tiyData.tParam.childrenParam.currentTime = new Date().getTime();

      tiyData.tStatus.modalChildren.type = 1;
      tiyData.tStatus.modalChildren.okText = "发布";
      tiyData.tStatus.modalChildren.isShow = true;
    }
  });
}

export const beforePreviewStands = (self, type, callback) => {
  let stands = type == common_data.standsDic.htmlType ? tiyData.tData.htmlBaseInfo : tiyData.tData.baseInfo;
  if(stands.type != 100 && !stands.shortLink){
    self.$Message.error("展厅没有固定网址，不能预览！");
    return;
  }
  let standsId = stands.id;
  let params = {id: standsId, state: common_data.standsDic.unPublishedState};
  self.$Spin.show();
  self.$apiStand.queryChildShowroomList(params).then(response => {
    self.$Spin.hide();
    if(response.result == null || response.result.length == 0) {//没有关联已发布展厅
      previewStands(self, type, stands.id, callback);
    }else{
      tiyData.tParam.childrenParam.showroomId = standsId;
      tiyData.tParam.childrenParam.currentTime = new Date().getTime();

      tiyData.tStatus.modalChildren.type = 2;
      tiyData.tStatus.modalChildren.okText = "预览";
      tiyData.tStatus.modalChildren.isShow = true;
    }
  });
}

export const previewStands = (self, type, standsId, callback) => {

  self.$Spin.show();
  let previewUrl = self.$store.getters.webDomain+"standInit/prepareStandRoom.do?code=li_6334522&type="+type+"&standRoomId="+standsId;
  self.$apiStand.previewStand(previewUrl,{}).then(response => {
    if(response.success) {
      self.$Message.success("生成页面成功！");
      if(callback) {
        callback(self);
      }
      window.open(self.$store.getters.webDomain+'tmp/'+response.result, "_blank")
    }else{
      self.$Message.success(response.errorMessage);
    }
    self.$Spin.hide();
  });
}

export const beforePublishPage = (self, callback) => {
  let stands = tiyData.tData.baseInfo;
  if(stands.type != 100 && !stands.shortLink){
    self.$Message.error("展厅没有固定网址，不能发布！");
    return;
  }
  let standsId=stands.id;

  let params = {id: standsId, state: commonData.standsDic.unPublishedState};
  self.$Spin.show();
  self.$apiStand.queryChildShowroomList(params).then(response => {
    self.$Spin.hide();
    if(response.result == null || response.result.length == 0) {//没有关联已发布展厅
      publishPage(self, callback);
    }else{
      tiyData.tParam.childrenParam.showroomId = standsId;
      tiyData.tParam.childrenParam.currentTime = new Date().getTime();

      tiyData.tStatus.modalChildren.type = 3;
      tiyData.tStatus.modalChildren.okText = "发布";
      tiyData.tStatus.modalChildren.isShow = true;
    }
  });
}

export const publishPage = (self, callback) => {
  let stands = tiyData.tData.baseInfo;
  let type = stands.type;
  let shortLink = "";
  if(type==100){
    shortLink = "index.html";
  }else{
    shortLink= encodeURIComponent(stands.shortLink);
  }
  self.$Spin.show();
  let publishPageUrl = self.$store.getters.webDomain+"standInit/publishStandRoom.do?code=li_6334522&fileName="+shortLink+"&standRoomId="+stands.id;

  ApiBase.send(publishPageUrl,{}).then(response => {
      if(response.success) {
        self.$Message.success("页面发布成功！");
        if(callback) {
          callback(self);
        }
      }else{
        let errorMsg = "页面发布失败！";
        if(response.errorMessage){
          errorMsg += response.errorMessage;
        }
        self.$Message.error(errorMsg);
      }
      self.$Spin.hide();
    }

  )
}

export const onlineBooth = (self, boothId) => {
  self.$Modal.confirm({
    title:"确认",
    content: "确定要将此展区上架吗？",
    onOk() {
      self.$Spin.show();
      self.$apiStand.onlineBooth({id: boothId}).then(response => {
        self.$Spin.hide();
        if(response.success) {
          self.$Message.success("展区上架成功！");
          queryBoothList(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
      }).catch(() => {
        self.$Spin.hide();
      });
    }
  });
}

export const offlineBooth = (self, boothId) => {
  self.$Modal.confirm({
    title:"确认",
    content: "确定要将此展区下架吗？",
    onOk() {
      self.$Spin.show();
      self.$apiStand.offlineBooth({id: boothId}).then(response => {
        self.$Spin.hide();
        if(response.success) {
          self.$Message.success("展区下架成功！");
          queryBoothList(self);
        }else{
          self.$Message.error(response.errorMessage);
        }
      }).catch(() => {
        self.$Spin.hide();
      });
    }
  });
}

export const initHtmlStandsInfo = (self, standsId) => {
  let params = {id: standsId};
  self.$Spin.show();
  self.$apiStand.queryStandsById(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      let stands = response.result;
      if(stands == null) {
        self.$Message.error("未查询到数据，展厅可能不存在或已被删除！");
        return;
      }
      //基本信息
      initHtmlBaseInfo(self, stands);
    }else{
      console.error(response.errorMessage);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("查询展厅信息失败！")
  });
}

export const initHtmlBaseInfo = (self, stands) => {
  tiyData.tData.htmlBaseInfo.id = stands.id;
  tiyData.tData.htmlBaseInfo.name = stands.name;
  tiyData.tData.htmlBaseInfo.platform = stands.platform;
  tiyData.tData.htmlBaseInfo.type = stands.type;
  tiyData.tData.htmlBaseInfo.state = stands.state;
  tiyData.tData.htmlBaseInfo.publishState = stands.publishState;
  tiyData.tData.htmlBaseInfo.shortLink = stands.shortLink;
  if(isNotEmpty(stands.shortLink)) {
    tiyData.tData.htmlBaseInfo.shortLinkName = stands.shortLink.substring(0, stands.shortLink.lastIndexOf(".html"));
  }else{
    tiyData.tData.htmlBaseInfo.shortLinkName = "";
  }

  tiyData.tData.htmlBaseInfo.tags.splice(0);
  if(isNotEmpty(stands.tags)) {
    tiyData.tData.htmlBaseInfo.tags.push(...self.$toJson(stands.tags));
  }
  tiyData.tData.htmlBaseInfo.note = stands.note;

  if(stands.htmlPage != null) {
    tiyData.tData.htmlPage.id = stands.htmlPage.id;
    tiyData.tData.htmlPage.showroomId = stands.htmlPage.showroomId;
    tiyData.tData.htmlPage.content = stands.htmlPage.content;
    tiyData.tData.htmlPage.isCodeEdit = stands.htmlPage.isCodeEdit;
  }else{
    tiyData.tData.htmlPage.id = "";
    tiyData.tData.htmlPage.showroomId = "";
    tiyData.tData.htmlPage.content = "";
    tiyData.tData.htmlPage.isCodeEdit = false;
  }

  tiyData.tData.htmlBaseInfo.createtime = stands.createtime;
  tiyData.tData.htmlBaseInfo.lastModifyTime = stands.lastModifyTime;
  tiyData.tData.htmlBaseInfo.lastPublishTime = stands.lastPublishTime;

  tiyData.tData.htmlBaseInfo.disableSeo = stands.disableSeo;
  tiyData.tData.htmlBaseInfo.pageMetaInfos.splice(0);
  tiyData.tData.htmlBaseInfo.pageMetaInfos.push(...stands.pageMetaInfos);
}

export const saveHtmlPage = (self) => {
  let params = tiyData.tParam.htmlPageParam;
  if(isEmpty(params.content)) {
    self.$Message.error("html页面内容不能为空！");
    return;
  }

  params.content = replaceHtmlTagAttr(params.content, "img", "width", "auto");
  params.content = replaceHtmlTagAttr(params.content, "img", "height", "auto");

  self.$Spin.show();
  self.$apiStand.saveOrUpdateHtmlPage(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("保存成功！");
      tiyData.tStatus.stands.htmlEditor.isShow = false;
      initHtmlStandsInfo(self, params.showroomId);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("保存失败！")
  })
}
